package com.lfy.modules.blog.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lfy.aop.anno.CheckFile;
import com.lfy.aop.enums.LegalFileTypeEnum;
import com.lfy.modules.common.config.sys.DictionariesKey;
import com.lfy.modules.common.config.sys.SysDictionaries;
import com.lfy.modules.blog.domain.HomeDetail;
import com.lfy.modules.blog.domain.SocialEntity;
import com.lfy.modules.blog.domain.dto.home.PersonalDTO;
import com.lfy.modules.blog.domain.event.ClearHomeArticleCacheEvent;
import com.lfy.modules.blog.service.HomeDetailService;
import com.lfy.modules.common.anno.AutoUser;
import com.lfy.modules.common.custom.constants.FileConstant;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.domain.entity.UserEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Optional;

@RequestMapping("/admin/personal")
@RestController
public class PersonalController {
    @Resource
    private HomeDetailService homeDetailService;

    /**
     * 上传头像
     * @param file 头像文件
     * @return R
     */
    @PostMapping("/up/headerImg")
    @CheckFile({LegalFileTypeEnum.JPG,LegalFileTypeEnum.PNG})
    public R<String> upHeaderImg(@RequestParam("file") MultipartFile file, @AutoUser UserEntity user) {

        if (file.getSize() > FileConstant.MAX_IMG_FILE_SIZE) {
            return R.error("图片大小不能超过10M");
        }
        String contentType = file.getContentType();

        if (contentType == null || !contentType.startsWith(FileConstant.IMG_FILE_PREFIX)) {
            return R.error("图片格式不正确");
        }
        R<String> r = homeDetailService.upHeaderImg(file);
        SpringUtil.publishEvent( new ClearHomeArticleCacheEvent(this,user.getId()));
        return r;
    }

    /**
     * 上传背景图
     * @param file 背景图文件
     * @return R
     */
    @PostMapping("/up/fm")
    public R<Void> upFm(@RequestParam("file") MultipartFile file, @AutoUser UserEntity user) {

        if (file.getSize() > FileConstant.MAX_IMG_FILE_SIZE) {
            return R.error("图片大小不能超过10M");
        }
        String contentType = file.getContentType();

        if (contentType == null || !contentType.startsWith(FileConstant.IMG_FILE_PREFIX)) {
            return R.error("图片格式不正确");
        }
        R<Void> r = homeDetailService.upFm(file);
        SpringUtil.publishEvent( new ClearHomeArticleCacheEvent(this,user.getId()));
        return r;
    }

    /**
     * 修改用户主页web详情
     * @param dto 用户详情
     * @param user 用户
     * @return R
     */
    @PostMapping("/modify/userDetail")
    public R<Void> modifyUserDetail(@Validated @RequestBody PersonalDTO dto, @AutoUser UserEntity user) {
        R<Void> r = homeDetailService.modifyUserDetail(dto);
        SpringUtil.publishEvent( new ClearHomeArticleCacheEvent(this,dto.getId()));
        return r;
    }

    /**
     * 获取用户主页web详情
     * @param user 用户
     * @return R
     */
    @GetMapping("/get/userDetail")
    public R<HomeDetail> getUserDetail(@AutoUser UserEntity user) {
        HomeDetail one = homeDetailService.getOne(new LambdaQueryWrapper<HomeDetail>().eq(HomeDetail::getTenantId, user.getId()));
        if (one != null) {

            String prefix = SysDictionaries.getValue(DictionariesKey.SYS_MINIO_SITE);
            Optional.ofNullable(one.getAuthorHeaderImg()).ifPresent(s -> one.setAuthorHeaderImg(prefix.concat(s)));
            Optional.ofNullable(one.getFm()).ifPresent(s -> one.setFm(prefix.concat(s)));
        }
        return R.success(one);
    }
    /**
     * 获取社交信息和歌单id
     */
    @GetMapping("/get/socialInfo")
    public R<SocialEntity> getSocialInfo(@AutoUser UserEntity user) {
        return this.homeDetailService.getSocialInfo(user.getId());
    }
    /**
     * 修改社交信息和歌单id
     */
    @PostMapping("/modify/socialInfo")
    public R<Void> modifySocialInfo( @RequestBody SocialEntity socialEntity, @AutoUser UserEntity user) {
        return this.homeDetailService.modifySocialInfo(socialEntity, user.getId());
    }
}
